<?php

import('classes.handler.Handler');
class SDTKHandler extends Handler {
	function KT_SDTK($args, &$request) {

		$this->validate();
		$this->setupTemplate(true);
		import('classes.acct.form.SDTKForm');
		$SDTKForm = new SDTKForm(!isset($args) || empty($args) ? null : ( $args[0]));
		$templateMgr =& TemplateManager::getManager();
		$xYear  = Request::getUserVar("xNam");
		if (!$xYear)
			$xYear = Request::getUserVar('nam');
	
		$xMode = Request::getUserVar("EditMode");

		if (!$xMode)
		{
			$xMode = "view";
		}

		if ($xMode == "save")
		{
		// Luwu 
			$SDTKForm->validate();
			$SDTKForm->readInputData();
			$SDTKForm->execute($xYear);

			$newurl = Request::url(null, null, null, null, array("nam"=>$xYear, "EditMode" => 'view'));
			PKPRequest::redirectUrl($newurl);
		}
		else if ($xMode == "edit")
		{

			$matk  = Request::getUserVar("matk");
			$templateMgr->assign('matk', $matk);

			$mant  = Request::getUserVar("mant");
			$templateMgr->assign('mant', $mant);
		}
	

		$user =& Request::getUser();
		$userId = $user->getId();
		$xma_dvcs = $user->getPath();
	
		$searchType = null;
		$searchMatch = null;
		$search = Request::getUserVar('search');
		$searchInitial = Request::getUserVar('searchInitial');
		if (!empty($search)) {
			$searchType = Request::getUserVar('searchField');
			$searchMatch = Request::getUserVar('searchMatch');

		} elseif (!empty($searchInitial)) {
			$searchInitial = String::strtoupper($searchInitial);
			$searchType = USER_FIELD_INITIAL;
			$search = $searchInitial;
		}

		$fieldOptions = Array(
		SDTK_FIELD_MATK => 'M&#227; TK',
			);

		$templateMgr->assign('fieldOptions', $fieldOptions);

		$templateMgr->assign('searchField', $searchType);
		$templateMgr->assign('searchMatch', $searchMatch);
		$templateMgr->assign('search', $search);
		$templateMgr->assign('searchInitial', Request::getUserVar('searchInitial'));


		$SDTKDao =& DAORegistry::getDAO('SDTKDAO');
		$dict = NewDataDictionary($SDTKDao->_dataSource);
		// Make sure the table exists
		$tables = $dict->MetaTables('TABLES', false);
		$journal =& Request::getJournal();
		$journalid =$journal->getId();

		$tablename = "sdtk".$journalid. $xYear;
		
		if (!in_array($tablename,$tables))
		{
			// chua co file SDTK, phai tao lai
			$SDTKDao->CreateTable($xYear, $journalid );
	
		}

		$SDTKs =& $SDTKDao->getSDTKSearch($journalid.$xYear, $searchType, $search, $searchMatch);
		if ($SDTKs->records == NULL)
			$templateMgr->assign('dtE', 0);	
		else
			$templateMgr->assign('dtE', 1);	

		$templateMgr->assign_by_ref('datas', $SDTKs);
		
		$templateMgr->assign('xMode', $xMode);


		$dmtkDao =& DAORegistry::getDAO('DMTKDAO');
		$dstk =& $dmtkDao->getDSMATK();
		$sa = implode(",", $dstk);
		$templateMgr->assign('dmtks', $sa);

		

		$templateMgr->addJavaScript("lib/pkp/js/lib/jquery/jquery.min.js");
		$templateMgr->addJavaScript('classes/acct/js/SDTK.js');
		$templateMgr->addStyleSheet($request->getBaseUrl().'/styles/dm.css');

		$templateMgr->assign('host', $SDTKDao->_dataSource->host);
		$templateMgr->assign('username', $SDTKDao->_dataSource->user);
		$templateMgr->assign('password', $SDTKDao->_dataSource->password);
		$templateMgr->assign('databaseName', $SDTKDao->_dataSource->databaseName);
		$templateMgr->assign('KT_SecDetail', "KT_SDTK");
		$templateMgr->assign('xNam', $xYear);
		$templateMgr->assign('xNamT', $xYear-1);

		$dmtkDao =& DAORegistry::getDAO('DMTKDAO');
		$dstk =& $dmtkDao->getDSMATK();
	

		$templateMgr->assign('taikhoan', $dstk);
		$templateMgr->assign('loaitienas', $dsmant);
		$templateMgr->assign('loaitien', 'VND');
		$templateMgr->assign('journalid', $journalid);

		// Duongdan den u_SDTK:
		$newurl = PKPRequest::getProtocol()."://". PKPRequest::getServerHost().PKPRequest::getBasePath()."/pages/acctKT/i_SDTK.php";
		$templateMgr->assign('urlIns', $newurl);

		
		

		$newurl = PKPRequest::getProtocol()."://". PKPRequest::getServerHost().PKPRequest::getBasePath()."/pages/acctKT/d_SDTK.php";

		$templateMgr->assign('urlDel', $newurl );
		$SDTKForm->display($templateMgr);
		return;
	}

	function zKT_SDTK($args, &$request) {

		$this->validate();
		$this->setupTemplate(true);
		import('classes.acct.form.SDTKForm');

		$SDTKForm = new SDTKForm(!isset($args) || empty($args) ? null : ( $args[0]));

		$templateMgr =& TemplateManager::getManager();


		$xYear  = Request::getUserVar("xNam");
		if (!$xYear)
			$xYear = Request::getUserVar('nam');
		

		$xMode = Request::getUserVar("EditMode");

		if (!$xMode)
		{
			$xMode = "view";
		}

		if ($xMode == "save")
		{
	// Luwu 
			$SDTKForm->validate();
			$SDTKForm->readInputData();
			$SDTKForm->execute($xYear);

			$newurl = Request::url(null, null, null, null, array("nam"=>$xYear, "EditMode" => 'view'));
			PKPRequest::redirectUrl($newurl);
		}
		else if ($xMode == "edit")
		{

			$matk  = Request::getUserVar("matk");
			$templateMgr->assign('matk', $matk);

			$mant  = Request::getUserVar("mant");
			$templateMgr->assign('mant', $mant);
		}
	

		$user =& Request::getUser();
		$userId = $user->getId();
		$xma_dvcs = $user->getPath();
	
		$searchType = null;
		$searchMatch = null;
		$search = Request::getUserVar('search');
		$searchInitial = Request::getUserVar('searchInitial');
		if (!empty($search)) {
			$searchType = Request::getUserVar('searchField');
			$searchMatch = Request::getUserVar('searchMatch');

		} elseif (!empty($searchInitial)) {
			$searchInitial = String::strtoupper($searchInitial);
			$searchType = USER_FIELD_INITIAL;
			$search = $searchInitial;
		}

		$fieldOptions = Array(
		SDTK_FIELD_MATK => 'M&#227; TK',
		SDTK_FIELD_MADV => 'Ma DV'
			);

		$templateMgr->assign('fieldOptions', $fieldOptions);

		$templateMgr->assign('searchField', $searchType);
		$templateMgr->assign('searchMatch', $searchMatch);
		$templateMgr->assign('search', $search);
		$templateMgr->assign('searchInitial', Request::getUserVar('searchInitial'));

		$SDTKDao =& DAORegistry::getDAO('SDTKDAO');
		$SDTKDao->ExistTable($xYear);

//		if (!$SDTKDao->ExistTable($xYear))
//		{
//			//nhattdk:  // tao table tu SDTK
//			$SDTKDao->CreateTable($xYear);
//			$xYear = ""; //nhattdk
//		}



		$SDTKs =& $SDTKDao->getSDTKSearch($xYear, $searchType, $search, $searchMatch);
		$templateMgr->assign_by_ref('datas', $SDTKs);
		
		$templateMgr->assign('xMode', $xMode);

		$dmntDao =& DAORegistry::getDAO('DMNTDAO');
		$dsmant =& $dmntDao->getDSMANT();
		$sa = implode(",", $dsmant);
		$templateMgr->assign('dmnts', $sa);


		$dmtkDao =& DAORegistry::getDAO('DMTKDAO');
		$dstk =& $dmtkDao->getDSMATK();
		$sa = implode(",", $dstk);
		$templateMgr->assign('dmtks', $sa);



		$dmdvDao =& DAORegistry::getDAO('DMDVDAO');
		$dstk =& $dmdvDao->getDSMADV();
		$sa = implode(",", $dstk);
		$templateMgr->assign('dmdvs', $sa);

		$templateMgr->addJavaScript("lib/pkp/js/lib/jquery/jquery.min.js");
		$templateMgr->addJavaScript('classes/acct/js/SDTK.js');
		$templateMgr->addStyleSheet($request->getBaseUrl().'/styles/dm.css');

		$templateMgr->assign('host', $SDTKDao->_dataSource->host);
		$templateMgr->assign('username', $SDTKDao->_dataSource->user);
		$templateMgr->assign('password', $SDTKDao->_dataSource->password);
		$templateMgr->assign('databaseName', $SDTKDao->_dataSource->databaseName);
		$templateMgr->assign('KT_SecDetail', "KT_SDTK");
		$templateMgr->assign('xNam', $xYear);
		$templateMgr->assign('xNamT', $xYear-1);

		$dmtkDao =& DAORegistry::getDAO('DMTKDAO');
		$dstk =& $dmtkDao->getDSMATK("11");
		$dmntDao =& DAORegistry::getDAO('DMNTDAO');
		$dsmant =& $dmntDao->getDSMANT_MA();


		$templateMgr->assign('tktienas', $dstk);
		$templateMgr->assign('loaitienas', $dsmant);
		$templateMgr->assign('loaitien', 'VND');

		// Duongdan den u_SDTK:
		$newurl = PKPRequest::getProtocol()."://". PKPRequest::getServerHost().PKPRequest::getBasePath()."/pages/acctKT/i_SDTK.php";
		$templateMgr->assign('urlIns', $newurl);

		
		$newurlRFR = Request::url(null, null, 'CTTMTG', null, array('mant' => $xmant, 'noco' => $noco, 'loaitk' =>$loaitk, 'ChiTiet' => $xChiTiet, 'ChiTiet' => $xChiTiet, 'Nam' => $gcNam, "ngay1"=> $ngay1, 'ngay2'=>$ngay2 ));
		$templateMgr->assign('urlRFR', $newurlRFR );

		$newurl = PKPRequest::getProtocol()."://". PKPRequest::getServerHost().PKPRequest::getBasePath()."/pages/acctKT/d_SDTK.php";

		$templateMgr->assign('urlDel', $newurl );


		$SDTKForm->display($templateMgr);
		return;
	}

	function deleteSDTK($args) {
		$xYear  = Request::getUserVar("xNam");
		if (!$xYear)
			$xYear = Request::getUserVar('nam');
		$id  = Request::getUserVar("id");
		
		$SDTKDao =& DAORegistry::getDAO('SDTKDAO');
		$SDTKDao->deleteSDTK($xYear, $id );
		
		$newurl = Request::url(null, null, "KT_SDTK", null, array("nam"=>$xYear, "EditMode" => 'view'));
		
		PKPRequest::redirectUrl($newurl);
	}

	function createSDTK() {
		$this->editSDTK();
	}

	function editSDTK($args = array()) {
		$this->validate();
		$this->setupTemplate(true);

		import('classes.acct.form.DMTKForm');

		$DMTKForm = new DMTKForm(!isset($args) || empty($args) ? null : ( $args[0]));

		if ($DMTKForm->isLocaleResubmit()) {
			$DMTKForm->readInputData();
		} else {
			$DMTKForm->initData();
		}
		//var_dump($DMTKForm);
		$DMTKForm->display();
	}



	function execute() {

			$DMTKDao =& DAORegistry::getDAO('DMTKDAO');

			if (isset($this->DMTKId)) {
				$DMTK =& $DMTKDao->getDMTK($this->DMTKId, false, false);
			}

			if (!isset($DMTK)) {
				$DMTK = new DMTK();
			}

			$DMTK->setmatk($this->getData('matk'), null); // Localized
			$DMTK->settentk($this->getData('tentk'), null); // Localized

			
			if (isset($this->DMTKId)) {
				$DMTKDao->updateDMTK($DMTK);
				$DMTKId = $DMTK->getId();

			} else {

				$DMTKId = $DMTKDao->insertDMTK($DMTK);
				
			}
	}



	function setupTemplate($subclass = false) {
		AppLocale::requireComponents(array(LOCALE_COMPONENT_PKP_SUBMISSION, LOCALE_COMPONENT_PKP_READER));
		parent::setupTemplate(true);
		if ($subclass) {
			$templateMgr =& TemplateManager::getManager();
			$templateMgr->append('pageHierarchy', array(Request::url(null, 'manager', 'sections'), 'section.sections'));
		}
	}


}

?>
